Root.prototype.main_action = function() {
   res.data.title = "Welcome";
   
   if (session.user) {
      res.handlers.you = session.user;
      res.data.body = this.renderSkinAsString("main#overview");
   } else {
      res.data.body = this.renderSkinAsString("main");
   }
   
   renderSkin("main");
}

Root.prototype.register_action = function() {
   if (req.isPost()) {
      if (req.data.username && (req.data.password_1 == req.data.password_2) && req.data.firstname && req.data.lastname) {
         // Create new User
         try {
            var toAdd = new User(req.data.username, req.data.password_1, req.data.firstname, req.data.lastname);
         } catch (e) {
            res.message = e;
            res.redirect(this.href("register"));
         }
         
         root.add(toAdd);
         res.redirect(this.href(req.data.username));
      } else {
         // res.message survives the HTTP-redirect!
         res.message = "Could not create user! Please check your input.";
         res.redirect(this.href("register"));  
      }
   }
   
   res.data.title = "Register new User";
   res.data.body = this.renderSkinAsString("registerUser");
   
   renderSkin("main");
}

Root.prototype.signin_action = function() {
   if (req.isPost()) {
      if (req.data.username && req.data.password) {
         var user = this.get(req.data.username);
         if (user) {
            if (user.password == req.data.password) {
               session.login(user);
               res.redirect(user.href());
            } else {
               res.message = "Wrong password!";
               res.redirect(this.href("signin"));
            }
         } else {
            res.message = "User not found!";
            res.redirect(this.href("signin")); 
         }
      } else {
         res.message = "Invalid request!";
         res.redirect(this.href("signin"));
      }
   }
   res.data.title = "Sign in";
   res.data.body  = this.renderSkinAsString("signinUser");
   renderSkin("main"); 
}

Root.prototype.signout_action = function(){
   session.logout();
   res.redirect(root.href());
}